package com.UnionPayCenter.operation.controller;

import com.UnionPayCenter.entity.CouponEntity;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.operation.entity.Activity;
import com.UnionPayCenter.operation.entity.dto.ActivityDTO;
import com.UnionPayCenter.operation.entity.dto.ActivityQueryDTO;
import com.UnionPayCenter.operation.entity.vo.ActivityVO;
import com.UnionPayCenter.operation.entity.vo.OperationRewardsVO;
import com.UnionPayCenter.operation.enums.ActivityOperationTypeEnum;
import com.UnionPayCenter.operation.service.ActivityService;
import com.UnionPayCenter.util.Asserts;
import com.UnionPayCenter.util.DateFormatUtil;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 赠送现金类型controller
 */
@Slf4j
@Api(value = "运营活动", tags = "运营活动")
@CurrentManager
@RestController
@RequestMapping("/operation/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @CurrentShop
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(tags = "运营活动", httpMethod = "POST", value = "[后台]增加运营活动", response = Integer.class)
    public ResponseWrapper add(@ApiParam @RequestBody(required = false) ActivityDTO activityDTO) {
        Asserts.notNull(activityDTO, "运营活动");
        Asserts.naturalNumber(activityDTO.getShopId(), "店铺Id");
        Asserts.naturalNumber(activityDTO.getActivityTypeId(), "活动类型Id");
        Asserts.naturalNumber(activityDTO.getRewardsType(), "活动奖品类型");

        if (activityDTO.getTimeStart() == null) {
            activityDTO.setTimeStart(new Date());
        }

        if (activityDTO.getTimeEnd() == null) {
            activityDTO.setTimeEnd(DateFormatUtil.stringToDate("2099-01-01 00:00:00"));
        }

        if (activityDTO.getTimeStart().after(activityDTO.getTimeEnd())) {
            throw new IllegalArgumentException("活动开始时间必须早于结束时间");
        }

        return ResponseWrapper.succeed(activityService.addActivity(activityDTO));
    }

    @CurrentShop
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(tags = "运营活动", httpMethod = "POST", value = "[后台]删除运营活动", response = Boolean.class)
    public ResponseWrapper delete(@ApiParam @RequestBody(required = false) ActivityDTO activityDTO) {
        Asserts.notNull(activityDTO, "运营活动");
        Asserts.naturalNumber(activityDTO.getActivityId(), "活动Id");
        activityService.deleteActivity(activityDTO.getActivityId());
        return ResponseWrapper.succeed(true);

    }

    @CurrentShop
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ApiOperation(tags = "运营活动", httpMethod = "POST", value = "[后台]修改运营活动状态", response = Boolean.class)
    public ResponseWrapper updateStatus(@ApiParam @RequestBody(required = false) ActivityDTO activityDTO) {
        Asserts.notNull(activityDTO, "运营活动");
        Asserts.naturalNumber(activityDTO.getActivityId(), "活动Id");
        Asserts.notNull(activityDTO.getStatus(), "状态");
        activityService.updateActivityStatus(activityDTO);
        return ResponseWrapper.succeed(true);
    }

    @CurrentShop
    @RequestMapping(value = "/updateActivity", method = RequestMethod.POST)
    @ApiOperation(tags = "运营活动", httpMethod = "POST", value = "[后台]修改运营活动", response = Boolean.class)
    public ResponseWrapper updateActivity(@ApiParam @RequestBody(required = false) ActivityDTO activityDTO) {
        Asserts.notNull(activityDTO, "运营活动");
        Asserts.naturalNumber(activityDTO.getActivityId(), "活动Id");
        activityService.updateActivity(activityDTO);
        return ResponseWrapper.succeed(true);
    }

    /**
     * 前台查询活动信息
     *
     * @param activityQueryDTO
     * @return
     */
    @CurrentShop
    @CurrentManager(false)
    @RequestMapping(value = "/getActivty", method = RequestMethod.POST)
    @ApiOperation(tags = "运营活动", httpMethod = "POST", value = "[前台]查询运营活动", response = Activity.class)
    public ResponseWrapper getActivity(@ApiParam @RequestBody(required = false) ActivityQueryDTO activityQueryDTO) {
        Asserts.notNull(activityQueryDTO, "查询条件");
        Asserts.naturalNumber(activityQueryDTO.getPage(), "页码");
        Asserts.naturalNumber(activityQueryDTO.getPageSize(), "每页数量");
        if (activityQueryDTO.getStatus() == null) {
            activityQueryDTO.setStatus(1);
        }
        return ResponseWrapper.succeed(activityService.getActivityWithoutRewards(activityQueryDTO));
    }

    /**
     * 前台查询活动信息
     *
     * @param activityQueryDTO
     * @return
     */
    @CurrentShop
    @CurrentManager(false)
    @RequestMapping(value = "/getRegisterActivty", method = RequestMethod.POST)
    @ApiOperation(tags = "运营活动", httpMethod = "POST", value = "[前台]查询运营活动", response = Activity.class)
    public ResponseWrapper getRegisterActivity(@ApiParam @RequestBody(required = false) ActivityQueryDTO activityQueryDTO) {
        Asserts.notNull(activityQueryDTO, "查询条件");
        Asserts.naturalNumber(activityQueryDTO.getShopId(), "店铺Id");
        if (activityQueryDTO.getStatus() == null) {
            activityQueryDTO.setStatus(1);
        }
        activityQueryDTO.setOperationType(ActivityOperationTypeEnum.REGIST.code());
        List<ActivityVO> activityVOS = activityService.getActivityByType(activityQueryDTO);
        List<CouponEntity> couponEntities = Lists.newArrayList();
        for (ActivityVO activityVO : activityVOS) {
            List<OperationRewardsVO> operationRewardsVOS = activityVO.getOperationRewardsDTOs();
            for (OperationRewardsVO operationRewardsVO : operationRewardsVOS) {
                for (int i = 0; i < operationRewardsVO.getQuantity(); i++) {
                    couponEntities.add(operationRewardsVO.getCouponEntity());
                }
            }
        }
        return ResponseWrapper.succeed(couponEntities);
    }

    @CurrentShop
    @RequestMapping(value = "/findActivty", method = RequestMethod.POST)
    @ApiOperation(tags = "运营活动", httpMethod = "POST", value = "[后台]查询运营活动", response = ActivityVO.class)
    public ResponseWrapper findActivity(@ApiParam @RequestBody(required = false) ActivityQueryDTO activityQueryDTO) {
        Asserts.notNull(activityQueryDTO, "查询条件");
        Asserts.naturalNumber(activityQueryDTO.getPage(), "页码");
        Asserts.naturalNumber(activityQueryDTO.getPageSize(), "每页数量");
        return ResponseWrapper.succeed(activityService.getActivity(activityQueryDTO));
    }

    @CurrentShop
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    @ApiOperation(tags = "运营活动", httpMethod = "POST", value = "[后台]查询运营活动", response = ActivityVO.class)
    public ResponseWrapper findActivityById(@ApiParam @RequestBody(required = false) ActivityQueryDTO activityQueryDTO) {
        Asserts.notNull(activityQueryDTO, "查询条件");
        Asserts.naturalNumber(activityQueryDTO.getActivityId(), "活动Id");
        return ResponseWrapper.succeed(activityService.getActivityById(activityQueryDTO.getActivityId()));
    }

}
